/*
 * Dealer.java
 *
 * Copyright(c) 2009 - Kimnach
 *
 * Description:
 *      Initiative dealer. takes list of players and shuffles for order.
 *      Also handles removing and adding of players.
 *
 */

package initiative;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import player.Player;

/**
 *
 * @author dkimnach
 */
public class Dealer {

    private Random random = new Random();

    private ArrayList<Player> list;

    private ArrayList<Player> dead;

    private int counter = 0;

    // takes a list of names and shuffles them returns jumbled list
    public Dealer(ArrayList<Player> players) {
        list = new ArrayList<Player>(players);
        dead = new ArrayList<Player>();
    }

    public Dealer() {
        list = new ArrayList<Player>();
        dead = new ArrayList<Player>();
    }

    public ArrayList<Player> deal() {
        for (int i = 0; i < list.size(); i++) {
            Player p = list.get(i);
            if (p.isDead()) {
                list.remove(i);
                dead.add(p);
                i--; // size of list is now less one
            }
        }
        list.trimToSize();

        Collections.shuffle(list, random);

        list.addAll(dead);

        dead.clear();

        System.out.println("New initiatives dealt");
        counter++;
        return list;
    }

    public ArrayList<Player> planB(int thisone, int thatone) {
        System.out.println(list.get(thisone).getName() + " for " + list.get(thatone).getName());

        Collections.swap(list, thisone, thatone);

        return list;
    }

    public ArrayList<Player> getList() {
        System.out.println("Getting list");
        return list;
    }

    public ArrayList<Player> moveToBottom(int index) {
        Player p = list.remove(index);
        list.add(p);
        System.out.println("Moving " + p.getName() + " to bottom");
        return list;
    }

    public ArrayList<Player> moveToTop(int index) {
        Player p = list.remove(index);
        list.add(0, p);
        System.out.println("Moving " + p.getName() + " to top");
        return list;
    }

    public ArrayList<Player> killPlayer(int index) {
        int last = moveToBottom(index).size() - 1;

        Player p = list.get(last);
        System.out.println(p.getName() + " dies");
        p.die();
        list.set(last, p);
        return list;
    }

    public ArrayList<Player> revive(Color col) {
        for (int i = 0; i < list.size(); i++){
            Player p = list.get(i);

            if (p.getColor().equals(col)) {
                System.out.println(p.getName() + " revived");
                p.revive();
                list.set(i, p);
            }
        }
        
        return list;
    }

    public int getCounter() {
        return counter;
    }

    @Override
    public String toString() {
        String s = "Deck contents: \n";
        for (Player p : list) {
            s += p.getName() + " - " + p.isDead() +"\n";
        }
        s += counter + " initiatives dealt";
        return s;
    }
}
